<template>
  <!-- 添加或修改盘点计划对话框 -->
  <el-dialog :title="title" v-model="open" width="900px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="90px" style="padding-left: 10%;" v-loading="loading">
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="应用名称:" prop="applyName">
            <el-input v-model="form.applyName"></el-input>
          </el-form-item></el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="应用类型:" prop="applyType">
            <el-select v-model="form.applyType" value-key="" placeholder="请选择应用类型">
              <el-option v-for="item in app_type" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
          <el-form-item label="是否上架:" prop="display">
            <el-switch v-bind="{ ...switchProps }" v-model="form.display"></el-switch>
          </el-form-item>
          <el-form-item label="应用图标:" prop="applyLogo">
            
            <el-upload v-if="form.applyLogo" class="avatar-uploader el-upload" :headers="headers" accept="image/*" 
              :on-success="(res, file) => handleUploadSuccess(res, file, 'applyLogo')" :show-file-list="false"
              :action="uploadUrl">
              <el-image v-if="form.applyLogo" class="avatar" :src="form.applyLogo" lazy>

                <template #error>
                  <div class="image-slot">
                    <img class="avatar" src="@/assets/goods/order/img_failure.svg" alt="">
                  </div>
                </template>
              </el-image>
              <div v-else class="upload-text">点击上传主图</div>
              <div class="btn" v-if="form.applyLogo">
                <el-button type="primary"  @click.stop="deleteImg('applyLogo')">
                  <el-icon>
                    <Delete />
                  </el-icon>
                </el-button>
              </div>
            </el-upload>
            <VueImgCutter  v-else v-model="form.applyLogo" @getData="cutDown" />
          </el-form-item>

      <el-form-item label="应用说明:" prop="applyDescription">
        <el-input type="textarea" :rows="4" v-model="form.applyDescription"></el-input>
      </el-form-item>
      <el-form-item label="应用详情:" prop="applyDetails">
        <editor v-model="form.applyDetails" :min-height="152" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm" :loading="loading">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import apply from '@/api/system/apply';
import modelMixin from '@/mixins/modelMixin'
import { globalHeaders } from '@/utils/request';
import VueImgCutter from "@/components/VueImgCutter/index.vue";
import { uploadFile } from '@/api/system/oss';
export default {
  name: "addForm",
  mixins: [modelMixin],
  vcomponents: {
    VueImgCutter
  },
  data() {
    return {
      funcObj: apply,//方法对象
      uploadUrl: import.meta.env.VITE_APP_BASE_API + '/resource/oss/upload', // 上传的图片服务器地址
      fileList: [],
      headers: globalHeaders(),
      open: false,
      title: "添加应用",
      form: {
        display: 0,
        applyType: '1',
      },
      switchProps: {
        'active-value': '0',
        'inactive-value': '1',
        'active-text': '是',
        'inactive-text': '否',
        'inline-prompt':true
      },
      app_type: [],
      rules: {
        applyName: [{ required: true, message: '请输入应用名称', trigger: 'blur' }],
        applyType: [{ required: true, message: '请选择应用类型', trigger: 'change' }],
        applyDescription: [{ required: true, message: '请输入应用说明', trigger: 'blur' }],
        applyDetails: [{ required: true, message: '请输入应用详情', trigger: 'blur' }],
        applyLogo: [{ required: true, message: '请上传应用图标', trigger: 'blur' }],

      },
    }
  },
  created() {
    console.log('globalHeaders() :>> ', globalHeaders());
    this.getDicts(['app_type'])
  },
  methods: {
    add() {
      this.open = true;
      this.form = {
        display: '0',
        applyType: '1',
      }
    },
    edit(row) {
      this.open = true;
      this.form = {}
      this.getDetail({ id: row.id })
    },
    cancel() {
      this.open = false;
    },
    
    // 删除图片
    deleteImg(key) {
      this.form[key] = '';
    },
    // 上传成功
    handleUploadSuccess(res, file, key) {
      console.log('res :>> ', res);
      // 如果上传成功
      if (res.code == 200) {
        this.form[key] = res.data.url;
      } else {
        console.log(res);
      }
    },
    cutDown(data){
      uploadFile(data).then(res => {
        console.log('res :>> ', res);
        this.form.applyLogo = res.data.url;
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader,
.el-upload {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px !important;
  height: 178px;
  display: flex;
  margin-right: 10px;
}

.avatar-uploader:hover,
.el-upload:hover {
  border-color: #409eff;
}

.imgs {
  display: inline-block;
}

.avatar-uploader__img {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px !important;
  height: 178px;
  display: inline-block;
  margin-right: 10px;

  img {
    width: 178px !important;
    height: 178px;
    object-fit: fill;
  }
}

.btn {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.5);
  color: #fff;
  width: 178px !important;
  height: 178px;
  border-radius: 0 6px 0 6px;
  text-align: center;
  line-height: 178px;
}

.avatar-uploader:hover {

  // 鼠标悬浮显示按钮
  .btn {
    display: block;
  }

}

.upload-text {
  font-size: 14px;
  color: #8c939d;
  line-height: 178px;
  text-align: center;
  margin: 0 auto;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>